package com.servlet.req; /**
 * @author wzm
 * @create 2024-01-27 16:13
 * project_name:JavaWeb2401
 * @Description
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@WebServlet("/rs2")
public class ReqeustServlet02 extends HttpServlet {
    //通用的获取参数的方法
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通用的获取参数的方式无论get或post请求都可以使用
        doPost(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数组成的Map集合
      /*  Map<String, String[]> map = request.getParameterMap();

        //遍历map
        //遍历每一个key对应value
        for (String key : map.keySet()){
            //key
            System.out.println("key = " + key);
            //通过key获得value 可能是多个
            String[] values = map.get(key);

            //遍历多个值
            for (String val : values){
                System.out.println("val = " + val);
            }

        }*/
        
        //根据请求参数的名称获取值的数组
    /*    String[] hobbies = request.getParameterValues("hobby");
        for (String i : hobbies) {
            System.out.println("i = " + i);
        }
*/

        //根据请求参数的名称获取单值
        //post请求会存在中文乱码问题  get请求已经解决了

        //解决方案
        //设置请求对象的编码为utf-8
        request.setCharacterEncoding("utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String hobby = request.getParameter("hobby");//如果存在多个值只会获得一个
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("hobby = " + hobby);

        /*User user = new User(username,password);*/

        //在这处理完逻辑需要跳转到静态资源或动态资源







    }
}
